import Vue from 'vue'
import App from './App.vue'
import router from './router'
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
import axios from 'axios'
import './css/index.less'
import store from './store/store.js'
import qs from 'qs'
import baseURL from './api/global.js'
import { Loading,Message } from 'element-ui';
// --------------------------
// import FullCalendar from 'vue-full-calendar'
// import 'fullcalendar/dist/fullcalendar.css'
// Vue.use(FullCalendar)

Vue.prototype.$http = axios
Vue.use(ElementUI);
Vue.config.productionTip = false;

Vue.prototype.baseURL = baseURL.testPath
axios.defaults.baseURL =  baseURL.testPath


axios.defaults.timeout = 10000;
axios.interceptors.request.use(
  config=>{
    Loading.service({
      lock: true,
      text: '加载中……',
      spinner: 'el-icon-loading',
      background: 'rgba(0, 0, 0, 0.7)'
    })
    config.headers["tenantId"] = 1;
    config.headers["appId"] = 2;
    if (config.method === 'get') {
      config.paramsSerializer = function (params) {
        return qs.stringify(params, {
          arrayFormat: 'indices'
        })
      }
    }
    const tokenStr =JSON.parse(window.localStorage.getItem('XIE_XIN'))
    if(tokenStr){
      config.headers.Authorization='Bearer ' + tokenStr.access_token
    }else {
      config.headers.Authorization = "Basic cGFyazpwYXJr";
    }
    return config
  },
  error => {
    return Promise.reject(error)
  }
)
axios.interceptors.response.use(response=>{
  if (response) {
  let loadingInstance = Loading.service({});
  setTimeout(()=>{
    loadingInstance.close();
  },10)
    return response
  }
},
error => {
//String(error).toLowerCase().indexOf('timeout')
  if (error && error.stack.indexOf('timeout') > -1) {
    Message.error('请求超时')
    let loadingInstance = Loading.service({});
    setTimeout(() => {
      loadingInstance.close();
    }, 10)
  }
})

  Vue.filter('dateFormat', function (originVal) {
    const dt = new Date(originVal)
    const y = dt.getFullYear()
    const m = (dt.getMonth() + 1 + '').padStart(2, '0')
    const d = (dt.getDate() + '').padStart(2, '0')
    const hh = (dt.getHours() + '').padStart(2, '0')
    const mm = (dt.getMinutes() + '').padStart(2, '0')
    const ss = (dt.getSeconds() + '').padStart(2, '0')
    return `${y}-${m}-${d} ${hh}:${mm}:${ss}`
  })
new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')